class MainScene extends eui.Component implements  eui.UIComponent {
	private iScroller:eui.Scroller;
	private itemNum:number;//滚动项的数量
	private itemSize:number;//单个滚动的长度
	private curItemCount:number = 1;//当前滚动到第几项
	private touchStartPos: number;//触摸起始位置
	private touchDist: number;//当前触摸位置和起始位置的距离
	private bScrolling: Boolean = false;//滚动中
	public delayScroll: number = 300;//滚动时间
	private  creatRunImg:CreatRunImg;
	private bgGroup:eui.Group;
	private stageX:number;

	private homePage:HomePage;
	private homePage1:HomePage;
	private meetings:Meetings;
	private peoples:Peoples;
	private wchats:Wchats;
	private wchats1:Wchats;

	private page0:eui.Group;
	private page1:eui.Group;
	private page2:eui.Group;
	private page3:eui.Group;
	private page4:eui.Group;
	private page5:eui.Group;
	public constructor() {
		super();
		this.homePage1 = new HomePage();
		this.homePage = new HomePage();
		this.meetings = new Meetings();
		this.peoples = new Peoples();
		this.wchats = new Wchats();
		this.wchats1 = new Wchats();
	}

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}


	protected childrenCreated():void
	{
		super.childrenCreated();
        SK_GLOBAL.screenAuto(this);
		this.creatRunImg =  new CreatRunImg(this.bgGroup);
		this.creatRunImg.addMoveBg();
		this.bgGroup.scaleX = .6;
		this.bgGroup.scaleY = .6;
		//设置初始显示滚动页面
		this.iScroller.viewport.scrollH = 640;
		this.envents();
		this.toScroll();
		this.addPage();
	}
	//监听滚动事件
	private envents ():void{
		this.stage.addEventListener(egret.TouchEvent.TOUCH_BEGIN,this.changeStart,this);
		this.stage.addEventListener(egret.TouchEvent.TOUCH_END,this.changeEnd,this);
	}

	//iScroller加载完成
	private changeStart(evt:egret.TouchEvent):void{
		this.touchStartPos = this.iScroller.viewport.scrollH;
		this.stageX = evt.stageX;
        this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.touchMove, this);
		this.creatRunImg.moveStart(evt.stageX);
	}

	private touchMove(evt:egret.TouchEvent):void{
		this.iScroller.viewport.scrollH  = this.touchStartPos - evt.stageX + this.stageX;
		this.creatRunImg.isOnMove(evt.stageX);
	}
	//iScroller触发滑动
	private changeEnd(evt:egret.TouchEvent):void{

		let status:string;
		//防点击触发changeend

		if (this.bScrolling) {
			console.log('滚动中,关闭滚动')
			return;
		}
		this.bScrolling = true;
		//开启滚动效果
		// console.log('开启滚动效果');
		//dict是拖动结束后的滚动条水平坐标减去拖动前的滚动条水平坐标
		let dict: number = this.iScroller.viewport.scrollH - this.touchStartPos;
		if (dict > 200) {

		//播放下一个图片

			// console.log('下一张')
			this.curItemCount++;
			egret.Tween.get(this.iScroller.viewport).to({ scrollH: this.itemSize*this.curItemCount , ease: egret.Ease.quadOut }, this.delayScroll).call(()=>{
				this.bScrolling = false;
				//判断是否为最后一张当为最后一张的时候 ,将索引变为1
				if(this.curItemCount == 5){
					this.iScroller.viewport.scrollH = 640;
					this.curItemCount = 1;
				}

			});
			status = 'next';
		} else if (dict <-200) {

		//播放上一个图片
			this.curItemCount--;
			egret.Tween.get(this.iScroller.viewport).to({ scrollH: this.itemSize*this.curItemCount , ease: egret.Ease.quadOut }, this.delayScroll).call(()=>{
				this.bScrolling = false;
				//判断是否为最后一张当为第一张的时候 ,将索引变为4;
				if(this.curItemCount == 0){
					this.iScroller.viewport.scrollH = 2560;
					this.curItemCount = 4;
				}
			});;

			// console.log('上一张')
			status = 'prave' ;
		}else{
			egret.Tween.get(this.iScroller.viewport).to({ scrollH: this.itemSize*this.curItemCount , ease: egret.Ease.quadOut }, 300).call(()=>{
				this.bScrolling = false;
			});
			status = 'now';
		}
		this.creatRunImg.moveOver(status);
	}

	private toScroll():void{
		//获得滚动数量
		this.itemNum = this.iScroller.numChildren;
		//获得滚动的单个尺寸
		this.itemSize = this.iScroller.width;
		//调节滑动结束时滚出的速度。等于0时，没有滚动动画
		this.iScroller.throwSpeed = 0;
		// console.log('测试提交');
	}

	//将每个页面添加进去
	private addPage():void{
		this.page1.addChild(this.homePage);
		this.page2.addChild(this.peoples);
		this.page3.addChild(this.meetings);
		this.page4.addChild(this.wchats);
		this.page5.addChild(this.homePage1);
		this.page0.addChild(this.wchats1);
	}
	
}